﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class HoaDonDAO
    {
        #region 1   -   Queries

        public static bool CapNhatHoaDon(HoaDonDTO hoadonDTO)
        {
            HOA_DON temp = ChuyenTuHoaDonDTOSangHoaDon(hoadonDTO);
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();

            try
            {
                HOA_DON hoadon = myDB.HOA_DONs.Single(p => p.ID == hoadonDTO.ID);
                hoadon.ThoiGianChuyenHang = hoadonDTO.ThoiGianChuyenHang;
                hoadon.ThoiGianThanhToan = hoadonDTO.ThoiGianThanhToan;
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static int LayIDHoaDonCuoi()
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            int result = (from hd in myDB.HOA_DONs
                          select hd.ID).Skip(myDB.HOA_DONs.Count() - 1).FirstOrDefault();
            return result;
        }

        public static HoaDonDTO LayHoaDonCuoiCung()
        {
            return LayHoaDonTheoID(LayIDHoaDonCuoi());
        }

        public static HoaDonDTO LayHoaDonTheoID(int id)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            HOA_DON temp = myDB.HOA_DONs.Single(c => c.ID == id);
            if (temp != null)
            {
                HoaDonDTO hoadon = new HoaDonDTO(temp.ThanhVienMuaHang.Value,temp.MaGianHang.Value,temp.TinhTrangHoaDon.Value,temp.TongSoTienThanhToan.Value,temp.ThoiGianChuyenHang.Value,temp.ThoiGianThanhToan.Value,temp.ThoiGianLapHoatDon.Value);
                hoadon.ID = temp.ID;
                return hoadon;
            }
                
            return null;
        }

        public static List<HoaDonDTO> LayDanhSachHoaDonTheoMaGianHang(int magianhang)
        {
            List<HoaDonDTO> result = new List<HoaDonDTO>();
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var listHoaDon = from hoadon in myDB.HOA_DONs
                             where hoadon.MaGianHang == magianhang
                             select hoadon;

            foreach (HOA_DON item in listHoaDon)
            {
                HoaDonDTO temp = ChuyenTuHoaDonSangHoaDonDTO(item);
                result.Add(temp);
            }
            return result;
        }

        public static List<HoaDonDTO> LayDanhSachHoaDonTheoNguoiMua(int nguoimua)
        {
            List<HoaDonDTO> result = new List<HoaDonDTO>();
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var listHoaDon = from hoadon in myDB.HOA_DONs
                             where hoadon.ThanhVienMuaHang.Value == nguoimua
                             select hoadon;

            foreach (HOA_DON item in listHoaDon)
            {
                HoaDonDTO temp = ChuyenTuHoaDonSangHoaDonDTO(item);
                temp.ID = item.ID;
                result.Add(temp);
            }
            return result;
        }

        public static bool XoaDanhSachHoaDonTheoMaGianHang(int magianhang)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var listHoaDon = from hoadon in myDB.HOA_DONs
                             where hoadon.MaGianHang == magianhang
                             select hoadon;
            foreach(HOA_DON hoadon in listHoaDon)
            {
                myDB.HOA_DONs.DeleteOnSubmit(hoadon);
                myDB.SubmitChanges();
            }
            if (listHoaDon.Count() == 0)
                return true;
            else
                return false;
        }

        public static bool XoaDanhSachHoaDonTheoNguoiMua(int nguoimua)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var listHoaDon = from hoadon in myDB.HOA_DONs
                             where hoadon.ThanhVienMuaHang.Value == nguoimua
                             select hoadon;
            foreach (HOA_DON hoadon in listHoaDon)
            {
                myDB.HOA_DONs.DeleteOnSubmit(hoadon);
                myDB.SubmitChanges();
            }
            if (listHoaDon.Count() == 0)
                return true;
            else
                return false;
        }

        public static bool ThemHoaDon(HoaDonDTO hoadonDTO)
        {
            HOA_DON temp = ChuyenTuHoaDonDTOSangHoaDon(hoadonDTO);
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();

            try
            {
                myDB.HOA_DONs.InsertOnSubmit(temp);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool XoaHoaDonTheoID(int id)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            HOA_DON temp = myDB.HOA_DONs.Single(c => c.ID == id);
            try
            {
                myDB.HOA_DONs.DeleteOnSubmit(temp);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        #endregion

        #region 2   -   Function Helper

        public static HoaDonDTO ChuyenTuHoaDonSangHoaDonDTO(HOA_DON hoadon)
        {
            return new HoaDonDTO(hoadon.ThanhVienMuaHang.Value, hoadon.MaGianHang.Value,
                hoadon.TinhTrangHoaDon.Value, hoadon.TongSoTienThanhToan.Value,
                hoadon.ThoiGianChuyenHang.Value, hoadon.ThoiGianThanhToan.Value,
                hoadon.ThoiGianLapHoatDon.Value);
        }

        public static HOA_DON ChuyenTuHoaDonDTOSangHoaDon(HoaDonDTO hoadonDTO)
        {
            HOA_DON result = new HOA_DON();
            result.ID = hoadonDTO.ID;
            result.MaGianHang = hoadonDTO.MaGianHang;
            result.ThanhVienMuaHang = hoadonDTO.ThanhVienMuaHang;
            result.ThoiGianChuyenHang = hoadonDTO.ThoiGianChuyenHang;
            result.ThoiGianLapHoatDon = hoadonDTO.ThoiGianLapHoaDon;
            result.ThoiGianThanhToan = hoadonDTO.ThoiGianThanhToan;
            result.TinhTrangHoaDon = hoadonDTO.TinhTrangHoaDon;
            result.TongSoTienThanhToan = hoadonDTO.TongSoTienThanhToan;
            return result;
        }
        #endregion
    }
}
